package edu.scau.pro.hotel.service.impl;

import edu.scau.pro.hotel.entity.*;
import edu.scau.pro.hotel.mapper.*;
import edu.scau.pro.hotel.service.IHotelCheckoutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * (HotelCheckout)表服务实现类
 *
 * @author CJW
 * @since 2022-04-17 15:27:29
 */
@Service("hotelCheckoutService")
public class HotelCheckoutServiceImpl implements IHotelCheckoutService {
    @Resource
    private HotelCheckoutMapper hotelCheckoutMapper;
    @Resource
    private HotelCheckinMapper hotelCheckinMapper;
    @Resource
    private HotelRoomMapper hotelRoomMapper;
    @Resource
    private HotelTypeMapper hotelTypeMapper;
    @Resource
    private HotelCustomerMapper hotelCustomerMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param checkoutId 主键
     * @return 实例对象
     */
    @Override
    public HotelCheckout queryById(Integer checkoutId) {
        return this.hotelCheckoutMapper.queryById(checkoutId);
    }

    /**
     * 查询指定行
     *
     * @param hotelCheckout 筛选条件
     * @return 查询结果
     */
    @Override
    public List<HotelCheckout> queryAll(HotelCheckout hotelCheckout) {
        return this.hotelCheckoutMapper.queryAll(hotelCheckout);
    }

    /**
     *计算两个日期之间的天数
     *@param star
     *@param endDay
     *@return 天数
     */
    public  static Integer daysBetween(Date star,Date endDay){
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date nextDay=star;
        int i=0;
        while(nextDay.before(endDay)){//当明天不在结束时间之前是终止循环
            Calendar cld = Calendar.getInstance();
            cld.setTime(star);
            cld.add(Calendar.DATE, 1);
            star = cld.getTime();
            //获得下一天日期字符串
            nextDay = star;
            i++;
        }
        return i;
    }

    /**
     * 返回退房对话框界面显示信息
     *
     * @param checkinId 实例对象
     * @return 实例对象
     */
    @Override
    public CheckoutForm respondCheckoutForm(Integer checkinId){
        // 创建checkoutId对象
        CheckoutForm checkoutForm = new CheckoutForm();
        // 根据checkinId返回hotelCheckin实体
        HotelCheckin hotelCheckin = this.hotelCheckinMapper.queryById(checkinId);
        // 设置入住信息里的实际退房时间为当下时间
        hotelCheckin.setRealTimeLeave(new Date());
        // 设置退房界面的入住信息
        checkoutForm.setHotelCheckin(hotelCheckin);

        // 根据customerId返回customer实体
        HotelCustomer hotelCustomer = this.hotelCustomerMapper.queryById(hotelCheckin.getCustomerId());
        // 设置设置退房界面的客户信息
        checkoutForm.setHotelCustomer(hotelCustomer);

        // 设置截至实际退房时间的入住总金额
        int stayDays = daysBetween(hotelCheckin.getCheckinTime(),hotelCheckin.getRealTimeLeave());

        // 根据roomId返回hotelRoom实体
        HotelRoom hotelRoom = this.hotelRoomMapper.queryById(hotelCheckin.getRoomId());
        // 设置设置退房界面的房间信息（房间号）
        checkoutForm.setHotelRoom(hotelRoom);

        // 根据typeId返回hotelType实体
        HotelType hotelType = this.hotelTypeMapper.queryById(hotelRoom.getTypeId());
        // 设置设置退房界面的房型信息（房型名称、房间单价）
//        checkoutForm.setHotelType(hotelType);

        // 计算截至实际退房时间的入住总金额
        double currentTotalRoomMoney = hotelType.getPrice() * stayDays;
        checkoutForm.setTotalRoomMoney(currentTotalRoomMoney);

        // 计算并设置退房界面的退/补款
        double currentPaidMoney = currentTotalRoomMoney - hotelCheckin.getPayMoney();
        checkoutForm.setExtraMoney(currentPaidMoney);

        // 返回退房界面信息表
        return checkoutForm;
    }




    /**
     * 新增数据
     *
     * @param hotelCheckout 实例对象
     * @return 实例对象
     */
    @Override
    public HotelCheckout insert(HotelCheckout hotelCheckout) {
        int insertNum = this.hotelCheckoutMapper.insert(hotelCheckout);
        if(insertNum!=0){
            // 修改房型表中空余房间数量
            HotelCheckin hotelCheckin = this.hotelCheckinMapper.queryById(hotelCheckout.getCheckinId());
            HotelRoom hotelRoom = this.hotelRoomMapper.queryById(hotelCheckin.getRoomId());
            HotelType hotelType = this.hotelTypeMapper.queryById(hotelRoom.getTypeId());
            hotelType.setVacantRoomNum(hotelType.getVacantRoomNum()+1);
            this.hotelTypeMapper.update(hotelType);
            // 修改入住信息表中的订单状态
            hotelCheckin.setCheckinStatus("已完成");
            System.out.println(hotelCheckin);
            this.hotelCheckinMapper.update(hotelCheckin);
            //修改房间表的状态
            hotelRoom.setRoomStatus("清扫");
            this.hotelRoomMapper.update(hotelRoom);
        }
        return hotelCheckout;
    }

    /**
     * 修改数据
     *
     * @param hotelCheckout 实例对象
     * @return 实例对象
     */
    @Override
    public HotelCheckout update(HotelCheckout hotelCheckout) {
        this.hotelCheckoutMapper.update(hotelCheckout);
        return this.queryById(hotelCheckout.getCheckoutId());
    }

    /**
     * 通过主键删除数据
     *
     * @param checkoutId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer checkoutId) {
        return this.hotelCheckoutMapper.deleteById(checkoutId) > 0;
    }
}
